using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using LeeAnn;
using LeeAnn.FeedForward;
using LeeAnn.Framework;
using System.Threading;

namespace Demo1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        DrawGeometry dGeo;
        Texture2D texDean;
        Texture2D texApple;
        List<Vector2D> AppleLocation;
        SpriteFont font;
        Color[,] DeanColor;
        Color[,] AppleColor;
        public bool lowProcessMode = false;
        public List<DeanCreature> creatures;
        control window;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            Content.RootDirectory = "Content";

            
        }

        protected override void Initialize()
        {
            base.Initialize();
            creatures = new List<DeanCreature>();
            AppleLocation = new List<Vector2D>();
            for (int i = 0; i < 5; i++)
                creatures.Add(new DeanCreature(Window.ClientBounds.Width, Window.ClientBounds.Height));
            for (int i = 0; i < 4;i++ )
                AppleLocation.Add(new Vector2D((float)Maths.RandomClamped(0, Window.ClientBounds.Width), (float)Maths.RandomClamped(0, Window.ClientBounds.Height)));
            window = new control();
            window.Show();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            dGeo = new DrawGeometry(this);

            texDean = this.Content.Load<Texture2D>("pacman");
            texApple = this.Content.Load<Texture2D>("apple_2");
            DeanColor = GraphicalStuff.TextureTo2DArray(texDean);
            AppleColor = GraphicalStuff.TextureTo2DArray(texApple);

            font = Content.Load<SpriteFont>("myFont");

        }
        protected override void UnloadContent()
        {
            

        }
        public void MoveDean()
        {
             
        }
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.R))
            {
                for (int i = 0; i < creatures.Count; i++)
                {
                    creatures[i].InitBrain(creatures[i].Width, creatures[i].Height);
                }

            }
            if (Keyboard.GetState().IsKeyDown(Keys.OemPlus))
            {
                for (int i = 0; i < creatures.Count; i++)
                {
                    creatures[i].Brain.Reward();
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.OemMinus))
            {
                for (int i = 0; i < creatures.Count; i++)
                {
                    creatures[i].Brain.Punish();
                }
            }
            if(Keyboard.GetState().IsKeyDown(Keys.F))
            {
                //if (!graphics.IsFullScreen)
                        graphics.ToggleFullScreen();
            }
            if (Keyboard.GetState().IsKeyDown(Keys.M) && AppleLocation.Count > 1)
                AppleLocation.RemoveAt(AppleLocation.Count - 1);
            if (Keyboard.GetState().IsKeyDown(Keys.A))
                AppleLocation.Add(new Vector2D((float)Maths.RandomClamped(0, Window.ClientBounds.Width), (float)Maths.RandomClamped(0, Window.ClientBounds.Height)));
            for (int i = 0; i < creatures.Count;i++ )
            {
                creatures[i].Update(ref AppleLocation, texDean, texApple, DeanColor, AppleColor);
            }

                base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            if (lowProcessMode)
                return;
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            int a = 0;
            foreach (DeanCreature c in creatures)
            {
                spriteBatch.Draw(texDean, new Vector2(c.Vec.X, c.Vec.Y), null, Color.White, c.Vec.Radians, new Vector2(texDean.Width / 2, texDean.Height / 2), 1f, SpriteEffects.None, 0);
                double tehangle = Maths.RoundClamp(Maths.PointsToAngle(c.Vec, AppleLocation[c.closestApple]) - c.Vec.Angle, -360, 360);
                spriteBatch.DrawString(font, a.ToString() + " - " + c.Rewards + "," + c.Punishments  , new Vector2(c.Vec.X + (texDean.Width / 2), c.Vec.Y + (texDean.Height / 2)), Color.White);
                dGeo.drawRectangle(new Rectangle((int)c.Vec.X, (int)c.Vec.Y, texDean.Width, texDean.Height), Color.Red,true);
                dGeo.drawLine(c.Vec, AppleLocation[c.closestApple],Color.Red);
                //LineBatch.DrawLine(spriteBatch, Color.Red, new Vector2(c.Vec.X, c.Vec.Y), new Vector2(AppleLocation[c.closestApple].X, AppleLocation[c.closestApple].Y));
                //LineBatch.DrawLine(spriteBatch, Color.Red, c.Vec, AppleLocation[c.closestApple]);
                a++;
                
            }
            //spriteBatch.Draw(texApple, new Microsoft.Xna.Framework.Rectangle((int)AppleLocation.X ,(int)AppleLocation.Y,texApple.Width,texApple.Height), Color.White);
            foreach(Vector2D app in AppleLocation)
                spriteBatch.Draw(texApple, new Vector2(app.X, app.Y), null, Color.White, 0f, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
            spriteBatch.End();
           
            base.Draw(gameTime);
        }

    }
}
